package a;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;

public class EssayGenerator {
    private static final String ESSAY_TEMPLATE_FILE = "a/essay_template.txt";
    private static final Random rand = new Random();

    public static void main(String[] args) {
        String essay = generateEssay();
        System.out.println(essay);
    }

    private static String generateEssay() {
        ArrayList<String> introTemplates = new ArrayList<>();
        ArrayList<String> bodyThemeTemplates = new ArrayList<>();
        ArrayList<String> bodyExpandTemplates = new ArrayList<>();
        ArrayList<String> conclusionTemplates = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(ESSAY_TEMPLATE_FILE))) {
            String line;
            int part = 0;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("1.")) {
                    part = 1;
                } else if (line.startsWith("2.")) {
                    part = 2;
                } else if (line.startsWith("3.")) {
                    part = 3;
                } else if (line.startsWith("-")) {
                    switch (part) {
                        case 1:
                            introTemplates.add(line.substring(2));
                            break;
                        case 2:
                            if (line.startsWith("- 主题句")) {
                                bodyThemeTemplates.add(line.substring(6));
                            } else if (line.startsWith("- 扩展句")) {
                                bodyExpandTemplates.add(line.substring(6));
                            }
                            break;
                        case 3:
                            conclusionTemplates.add(line.substring(2));
                            break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        StringBuilder essay = new StringBuilder();

        // 生成开头
        String intro = introTemplates.get(rand.nextInt(introTemplates.size()));
        essay.append(intro).append("\n\n");

        // 生成中间
        String theme = bodyThemeTemplates.get(rand.nextInt(bodyThemeTemplates.size()));
        essay.append(theme).append("\n");

        int numExpand = rand.nextInt(3) + 1; // 随机生成1-3个扩展句
        for (int i = 0; i < numExpand; i++) {
            String expand = bodyExpandTemplates.get(rand.nextInt(bodyExpandTemplates.size()));
            essay.append(expand).append("\n");
        }

        // 生成结尾
        String conclusion = conclusionTemplates.get(rand.nextInt(conclusionTemplates.size()));
        essay.append("\n").append(conclusion);

        return essay.toString();
    }
}
